home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 02 - 1986 / 02.12 Dec 86.sit / 02.12 Dec 86 / C Sources / dr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-10  |  5.2 KB  |  377 lines  |  [TEXT/KAHL]

  1. /* 
  2.  * dr.c
  3.  *
  4.  * drawing routines
  5.  */
  6.  
  7.  #include    "abc.h"
  8.  #include    "quickdraw.h"
  9.  #include    "windowMgr.h"
  10.  
  11. struct shapes
  12.     {
  13.     short    kind;
  14.     Rect    size;
  15.     short    oper;
  16.     };
  17.             
  18.  
  19. struct shapes       shapa[20];
  20. short                shapdx;
  21.  
  22.  
  23. fr_line(startpt,endpt)
  24.     Point    startpt,endpt;
  25. {
  26.     MoveTo(startpt.h,startpt.v);
  27.     LineTo(endpt.h,endpt.v);
  28. }
  29.  
  30. fr_rect(startpt,endpt)
  31.     Point    startpt,endpt;
  32. {
  33.     Rect    rt;
  34.     
  35.     Pt2Rect(startpt,endpt,&rt);
  36.     FrameRect(&rt);
  37. }
  38.  
  39. fr_oval(startpt,endpt)
  40.     Point    startpt,endpt;
  41. {
  42.     Rect    rt;
  43.     
  44.     Pt2Rect(startpt,endpt,&rt);
  45.     FrameOval(&rt);
  46. }
  47.  
  48. fr_rort(startpt,endpt)
  49.     Point    startpt,endpt;
  50. {
  51.     Rect    rt;
  52.     
  53.     Pt2Rect(startpt,endpt,&rt);
  54.     FrameRoundRect(&rt,20,20);
  55. }
  56.  
  57.  
  58. fr_arc(startpt,endpt)
  59.     Point    startpt,endpt;
  60. {
  61.     Rect    rt;
  62.     Rect    trt;
  63.     short    sa;
  64.     short    aa;
  65.     
  66.     Pt2Rect(startpt,endpt,&rt);
  67.     cp_arc(&rt,&trt,&sa,&aa);
  68.     FrameArc (&trt,sa,aa);
  69. }
  70.  
  71. cp_arc(irt,ort,startangle,arcangle)
  72.     Rect    *irt;
  73.     Rect    *ort;
  74.     short    *startangle;
  75.     short    *arcangle;
  76. {
  77.     short    dh;
  78.     short    dv;
  79.     static Point    anchor;
  80.     
  81.     dh = irt->right - irt->left;
  82.     dv = irt->bottom - irt->top;
  83.     if (not (dh | dv))
  84.         {
  85.         anchor.v = irt->top;
  86.         anchor.h = irt->left;
  87.         }
  88.     *ort = *irt;
  89.     
  90.     if (irt->left equals anchor.h)
  91.         if (irt->top < anchor.v)
  92.             {
  93.             ort->left -= dh;
  94.             ort->top -= dv;
  95.             *startangle = 180;
  96.             *arcangle = -90;
  97.             }
  98.         else
  99.             {
  100.             ort->left -= dh;
  101.             ort->bottom += dv;
  102.             *startangle = 0;
  103.             *arcangle = 90;
  104.             }
  105.     else
  106.         if (irt->top < anchor.v)
  107.             {
  108.             ort->top -= dv;
  109.             ort->right += dh;
  110.             *startangle = 180;
  111.             *arcangle = 90;
  112.             }
  113.         else
  114.             {
  115.             ort->right += dh;
  116.             ort->bottom += dv;
  117.             *startangle = 0;
  118.             *arcangle = - 90;
  119.             }
  120. }
  121.  
  122. er_line(startpt,endpt)
  123.     Point    startpt,endpt;
  124. {
  125.     GrafPtr        gp;
  126.     Pattern        tpat;
  127.     
  128.     GetPort(&gp);
  129.     BlockMove(gp->pnPat,&tpat,8);
  130.     PenPat(gp->bkPat);
  131.     MoveTo(startpt.h,startpt.v);
  132.     LineTo(endpt.h,endpt.v);
  133.     PenPat(&tpat);
  134. }
  135.  
  136. er_rect(startpt,endpt)
  137.     Point    startpt,endpt;
  138. {
  139.     Rect    rt;
  140.     Pt2Rect(startpt,endpt,&rt);
  141.     EraseRect(&rt);
  142. }
  143.  
  144. er_oval(startpt,endpt)
  145.     Point    startpt,endpt;
  146. {
  147.     Rect    rt;
  148.     
  149.     Pt2Rect(startpt,endpt,&rt);
  150.     EraseOval(&rt);
  151. }
  152.  
  153. er_rort(startpt,endpt)
  154.     Point    startpt,endpt;
  155. {
  156.     Rect    rt;
  157.     
  158.     Pt2Rect(startpt,endpt,&rt);
  159.     EraseRoundRect(&rt,20,20);
  160. }
  161.  
  162. er_arc(startpt,endpt)
  163.     Point    startpt,endpt;
  164. {
  165.     Rect    rt;
  166.     Rect    trt;
  167.     short    sa;
  168.     short    aa;
  169.     
  170.     Pt2Rect(startpt,endpt,&rt);
  171.     cp_arc(&rt,&trt,&sa,&aa);
  172.     EraseArc (&trt,sa,aa);
  173. }
  174.  
  175.  
  176. pt_line(startpt,endpt)
  177.     Point    startpt,endpt;
  178. {
  179.     GrafPtr        gp;
  180.     Pattern        tpat;
  181.     
  182.     MoveTo(startpt.h,startpt.v);
  183.     LineTo(endpt.h,endpt.v);
  184. }
  185.  
  186. pt_rect(startpt,endpt)
  187.     Point    startpt,endpt;
  188. {
  189.     Rect    rt;
  190.     
  191.     Pt2Rect(startpt,endpt,&rt);
  192.     PaintRect(&rt);
  193. }
  194.  
  195. pt_oval(startpt,endpt)
  196.     Point    startpt,endpt;
  197. {
  198.     Rect    rt;
  199.     
  200.     Pt2Rect(startpt,endpt,&rt);
  201.     PaintOval(&rt);
  202. }
  203.  
  204. pt_rort(startpt,endpt)
  205.     Point    startpt,endpt;
  206. {
  207.     Rect    rt;
  208.     
  209.     Pt2Rect(startpt,endpt,&rt);
  210.     PaintRoundRect(&rt,20,20);
  211. }
  212.  
  213.  
  214. pt_arc(startpt,endpt)
  215.     Point    startpt,endpt;
  216. {
  217.     Rect    rt;
  218.     Rect    trt;
  219.     short    sa;
  220.     short    aa;
  221.     
  222.     Pt2Rect(startpt,endpt,&rt);
  223.     cp_arc(&rt,&trt,&sa,&aa);
  224.     PaintArc (&trt,sa,aa);
  225. }
  226.  
  227.  
  228. in_line(startpt,endpt)
  229.     Point    startpt,endpt;
  230. {
  231.     GrafPtr        gp;
  232.     short        tpnMode;
  233.     
  234.     GetPort(&gp);
  235.     tpnMode = gp->pnMode;
  236.     PenMode(patXor);
  237.     MoveTo(startpt.h,startpt.v);
  238.     LineTo(endpt.h,endpt.v);
  239.     PenMode(tpnMode);
  240. }
  241.  
  242. in_rect(startpt,endpt)
  243.     Point    startpt,endpt;
  244. {
  245.     Rect    rt;
  246.     
  247.     Pt2Rect(startpt,endpt,&rt);
  248.     InvertRect(&rt);
  249. }
  250.  
  251. in_oval(startpt,endpt)
  252.     Point    startpt,endpt;
  253. {
  254.     Rect    rt;
  255.     
  256.     Pt2Rect(startpt,endpt,&rt);
  257.     InvertOval(&rt);
  258. }
  259.  
  260. in_rort(startpt,endpt)
  261.     Point    startpt,endpt;
  262. {
  263.     Rect    rt;
  264.     
  265.     Pt2Rect(startpt,endpt,&rt);
  266.     InvertRoundRect(&rt,20,20);
  267. }
  268.  
  269. in_arc(startpt,endpt)
  270.     Point    startpt,endpt;
  271. {
  272.     Rect    rt;
  273.     Rect    trt;
  274.     short    sa;
  275.     short    aa;
  276.     
  277.     Pt2Rect(startpt,endpt,&rt);
  278.     cp_arc(&rt,&trt,&sa,&aa);
  279.     InvertArc (&trt,sa,aa);
  280. }
  281.  
  282.     
  283. typedef    short    (*drfunc)();
  284.  
  285. drfunc            a[][5] = {fr_line,fr_rect,fr_oval,fr_rort,fr_arc,
  286.                           pt_line,pt_rect,pt_oval,pt_rort,pt_arc,
  287.                           er_line,er_rect,er_oval,er_rort,er_arc,
  288.                           in_line,in_rect,in_oval,in_rort,in_arc};
  289.  
  290.  
  291. drinit()
  292. {
  293.     short    i;
  294.     
  295.     for (i = 0; i < 20; shapa[i++].kind = 0)
  296.         ;
  297.     shapdx = 0;
  298.     
  299. }
  300.  
  301. drshape(code)
  302.     short    code;
  303. {
  304.     shapa[shapdx].kind = code;
  305. }
  306.  
  307. droper(code)
  308.     short    code;
  309. {
  310.     shapa[shapdx].oper = code;
  311. }
  312.  
  313. drsize(r)
  314.     Rect    *r;
  315. {
  316.     shapa[shapdx].size = *r;
  317. }
  318.  
  319. drdraw(w)
  320.     WindowRecord    *w;
  321. {
  322.     Point                startpt;
  323.     Point                thispt;
  324.     Point                endpt;
  325.     Point                lastpt;
  326.     Rect                thisrt;
  327.     Rect                lastrt;
  328.     GrafPtr                port;
  329.     drfunc                frame;
  330.     drfunc                draw;
  331.     short                angle;
  332.     short                dv,dh;
  333.     Point                sp;
  334.     Point                tp;
  335.     Point                lp;
  336.     short                shapx;
  337.     short                operx;
  338.     
  339.     SetPort((GrafPtr)w);
  340.     GetMouse(&startpt);
  341.     lastpt = startpt;
  342.     PenMode(patXor);
  343.     PenPat(gray);
  344.     shapx = shapa[shapdx].kind - 1;
  345.     operx = shapa[shapdx].oper - 1;
  346.     if ((shapx < 0) or (operx < 0)) /* to prevent trying */
  347.         return;                        /* to use unselected items */
  348.     frame = a[0][shapx];            /* get address of frame func */
  349.     draw  = a[operx][shapx];        /* get address of shape/oper func */
  350.     do    {
  351.         GetMouse(&endpt);
  352.         thispt = endpt;
  353.         LocalToGlobal(&endpt);
  354.         if (PtInRgn(endpt,w->contRgn) and 
  355.             not EqualPt(thispt,lastpt))
  356.             {
  357.             (*frame)(startpt,lastpt);
  358.             (*frame)(startpt,thispt);
  359.             lastpt = thispt;
  360.             }
  361.         }
  362.     while (StillDown());
  363.     (*frame)(startpt,thispt);
  364.     PenMode(patCopy);
  365.     PenPat(black);
  366.     (*draw)(startpt,thispt);    
  367. }
  368.  
  369.  
  370. abs(num)
  371.     short    num;
  372. {
  373.     if (num < 0 )
  374.         return -num;
  375.     return num;
  376. /*    num < 0 ? return -num : return num ;*/
  377. }